/**************************************************************************//**
 * @item     CosyOS-III Kernel
 * @file     os_api.h
 * @brief    系统内核专用API
 * @author   迟凯峰
 * @version  V1.2.1
 * @date     2025.04.27
 ******************************************************************************/

#ifndef __OS_API_H
#define __OS_API_H

void os_debug_recv(char *p, const size_t len);

/*
 * 引用.标准库函数
 */

#define s_init_mempool  init_mempool
#define s_malloc  malloc
#define s_calloc  calloc
#define s_realloc realloc
#define s_free    free
#define s_memcpy  memcpy
#define s_memcmp  memcmp
#define s_strcpy  strcpy
#define s_strcmp  strcmp
#define s_strlen  strlen



/*
 * 操作.定时中断/查询
 */

/** 启动定时中断任务 */
#define sStartTask_TimInt(tmid) \
	sUSV_StartTask(s_timint_handle[tmid]->hookorhand.hand, OS_STATUS_SUSPENDED)

/** 启动定时查询任务 */
#define sStartTask_TimQry(tmid) \
	sUSV_StartTask(s_timqry_handle[tmid]->hookorhand.hand, OS_STATUS_SUSPENDED)

/** 恢复定时中断任务 */
#define sResumeTask_TimInt(tmid) \
	sTSV_ResumeTask(*(s_timint_handle[tmid]->hookorhand.hand)->dualhandle)

/** 恢复定时查询任务 */
#define sResumeTask_TimQry(tmid) \
	sTSV_ResumeTask(*(s_timqry_handle[tmid]->hookorhand.hand)->dualhandle)

/** 调用定时中断钩子 */
#define sCallHook_TimInt(tmid) \
	(*s_timint_handle[tmid]->hookorhand.hook)()

/** 调用定时查询钩子 */
#define sCallHook_TimQry(tmid) \
	(*s_timqry_handle[tmid]->hookorhand.hook)()



/*
 * 更新.全局变量副本
 */

#define sUpdateCopy(code) \
do{ \
	s_sign_updatecopy = true; \
	code; \
	s_sign_updatecopy = false; \
}while(false)



/*
 * 事件标志组 - 写标志位
 */

#define stWriteFlagBits_1(g, v, b0) \
	g.b0 = v

#define stWriteFlagBits_2(g, v, b0, b1) \
	g.b0 = g.b1 = v

#define stWriteFlagBits_3(g, v, b0, b1, b2) \
	g.b0 = g.b1 = g.b2 = v

#define stWriteFlagBits_4(g, v, b0, b1, b2, b3) \
	g.b0 = g.b1 = g.b2 = g.b3 = v

#define stWriteFlagBits_5(g, v, b0, b1, b2, b3, b4) \
	g.b0 = g.b1 = g.b2 = g.b3 = g.b4 = v

#define stWriteFlagBits_6(g, v, b0, b1, b2, b3, b4, b5) \
	g.b0 = g.b1 = g.b2 = g.b3 = g.b4 = g.b5 = v

#define stWriteFlagBits_7(g, v, b0, b1, b2, b3, b4, b5, b6) \
	g.b0 = g.b1 = g.b2 = g.b3 = g.b4 = g.b5 = g.b6 = v

#define stWriteFlagBits_8(g, v, b0, b1, b2, b3, b4, b5, b6, b7) \
	g.b0 = g.b1 = g.b2 = g.b3 = g.b4 = g.b5 = g.b6 = g.b7 = v

#define suWriteFlagBits_1(g, v, b0) \
do{ \
	su_kernel_locks(); \
	stWriteFlagBits_1(g, v, b0); \
	su_kernel_unlocks(); \
}while(false)

#define suWriteFlagBits_2(g, v, b0, b1) \
do{ \
	su_kernel_locks(); \
	stWriteFlagBits_2(g, v, b0, b1); \
	su_kernel_unlocks(); \
}while(false)

#define suWriteFlagBits_3(g, v, b0, b1, b2) \
do{ \
	su_kernel_locks(); \
	stWriteFlagBits_3(g, v, b0, b1, b2); \
	su_kernel_unlocks(); \
}while(false)

#define suWriteFlagBits_4(g, v, b0, b1, b2, b3) \
do{ \
	su_kernel_locks(); \
	stWriteFlagBits_4(g, v, b0, b1, b2, b3); \
	su_kernel_unlocks(); \
}while(false)

#define suWriteFlagBits_5(g, v, b0, b1, b2, b3, b4) \
do{ \
	su_kernel_locks(); \
	stWriteFlagBits_5(g, v, b0, b1, b2, b3, b4); \
	su_kernel_unlocks(); \
}while(false)

#define suWriteFlagBits_6(g, v, b0, b1, b2, b3, b4, b5) \
do{ \
	su_kernel_locks(); \
	stWriteFlagBits_6(g, v, b0, b1, b2, b3, b4, b5); \
	su_kernel_unlocks(); \
}while(false)

#define suWriteFlagBits_7(g, v, b0, b1, b2, b3, b4, b5, b6) \
do{ \
	su_kernel_locks(); \
	stWriteFlagBits_7(g, v, b0, b1, b2, b3, b4, b5, b6); \
	su_kernel_unlocks(); \
}while(false)

#define suWriteFlagBits_8(g, v, b0, b1, b2, b3, b4, b5, b6, b7) \
do{ \
	su_kernel_locks(); \
	stWriteFlagBits_8(g, v, b0, b1, b2, b3, b4, b5, b6, b7); \
	su_kernel_unlocks(); \
}while(false)

#define siWriteFlagBits_1(b0) \
	stWriteFlagBits_1(u_grp, true, b0); \
	siWriteFlagBits()

#define siWriteFlagBits_2(b0, b1) \
	stWriteFlagBits_2(u_grp, true, b0, b1); \
	siWriteFlagBits()

#define siWriteFlagBits_3(b0, b1, b2) \
	stWriteFlagBits_3(u_grp, true, b0, b1, b2); \
	siWriteFlagBits()

#define siWriteFlagBits_4(b0, b1, b2, b3) \
	stWriteFlagBits_4(u_grp, true, b0, b1, b2, b3); \
	siWriteFlagBits()

#define siWriteFlagBits_5(b0, b1, b2, b3, b4) \
	stWriteFlagBits_5(u_grp, true, b0, b1, b2, b3, b4); \
	siWriteFlagBits()

#define siWriteFlagBits_6(b0, b1, b2, b3, b4, b5) \
	stWriteFlagBits_6(u_grp, true, b0, b1, b2, b3, b4, b5); \
	siWriteFlagBits()

#define siWriteFlagBits_7(b0, b1, b2, b3, b4, b5, b6) \
	stWriteFlagBits_7(u_grp, true, b0, b1, b2, b3, b4, b5, b6); \
	siWriteFlagBits()

#define siWriteFlagBits_8(b0, b1, b2, b3, b4, b5, b6, b7) \
	stWriteFlagBits_8(u_grp, true, b0, b1, b2, b3, b4, b5, b6, b7); \
	siWriteFlagBits()

#define siWriteFlagBits() \
		u_psv.value = ( \
			  sizeof(u_grp) == 1 ? *(s_u8_t  *)&u_grp \
			: sizeof(u_grp) == 2 ? *(s_u16_t *)&u_grp \
			: sizeof(u_grp) == 4 ? *(s_u32_t *)&u_grp \
			: 0 \
		); \
	} \
	mPendSV_FIFOLoad(); \
}while(false)



#endif
